#include"compile_run.hpp"
#include"../comm/httplib.h"

using namespace ns_compile_and_run;
using namespace httplib;

//main参数提示
void Usage(std::string proc)
{
    std::cerr<<"Usage: "<<"\n\t"<<proc<<" port"<<std::endl;
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }
    Server svr;

    // svr.Get("/hello",[](const Request&  req, Response& resq){
    //     resq.set_content("hello httplib! 肖", "text/plain;charset=utf-8;");
    // });

    //部署网络服务(将客户端请求的数据中的代码提取出来，交给编译运行模块)
    svr.Post("/compile_and_run",[](const Request&  req, Response& resq){
        std::string in_json = req.body;//body中存放着是有效正文
        std::string out_json;
        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json,&out_json);
            resq.set_content(out_json,"application/json;charset=utf-8");
        }
    });

    // svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0",atoi(argv[1]));
    // std::string in_json;
    // //充当一次客户端的请求
    // Json::Value in_value;
    // in_value["code"] =R"(
    //     #include<iostream>
    //     int main()
    //     {
    //         std::cout<<"hello test"<<std::endl;
    //         return 0;
    //     })";
    // in_value["input"] = "";
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 102400;

    // Json::FastWriter writer;
    // in_json = writer.write(in_value);
    // //std::cout<<in_json<<std::endl;

    // std::string out_json;
    // CompileAndRun::Start(in_json, &out_json);
    // std::cout<<out_json<<std::endl;
    return 0;
}